package com.canbot.u05.activity.agenda.dialog

import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import com.canbot.u05.R
import com.canbot.u05.activity.agenda.calendar.WheelStyle
import com.canbot.u05.activity.presidemode.newcode.view.NHostBaseDialog
import com.canbot.u05.utils.PathConst
import com.canbot.u05.utils.ToastTools
import kotlinx.android.synthetic.main.agenda_time_dialog.*
import kotlinx.android.synthetic.main.layout_time_picker.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * @author yinxiaowei
 * @date 2019/5/31
 */
class TimeDialog(context: Context, startTime: Long?, endTime: Long?) : NHostBaseDialog(context), View.OnClickListener {
    private val TAG = TimeDialog::class.java.simpleName
    private val voices = arrayOf(arrayOf("${PathConst.AGENDA_VOICE_DIR}gzsj_gzrc_u05_001,mp3", "43"),
            arrayOf("${PathConst.AGENDA_VOICE_DIR}gzsj_gzrc_u05_001,mp3", "12"))

    init {
        val view = LayoutInflater.from(this.context).inflate(R.layout.agenda_time_dialog, null)
        setContentView(view)
        startTime_HourNew.setWheelStyle(WheelStyle.STYLE_HOUR)
        startTime_MinuteNew.setWheelStyle(WheelStyle.STYLE_MINUTE)
        endTimeHourNew.setWheelStyle(WheelStyle.STYLE_HOUR)
        endTimeMinuteNew.setWheelStyle(WheelStyle.STYLE_MINUTE)

        if (startTime != null && endTime != null) {
            val calendar = Calendar.getInstance()
            calendar.timeInMillis = startTime
            val startHour = calendar.get(Calendar.HOUR_OF_DAY)
            val startMinute = calendar.get(Calendar.MINUTE)
            calendar.timeInMillis = endTime
            val endHour = calendar.get(Calendar.HOUR_OF_DAY)
            val endMinute = calendar.get(Calendar.MINUTE)
            startTime_HourNew.currentItem = startHour
            startTime_MinuteNew.currentItem = startMinute
            endTimeHourNew.currentItem = endHour
            endTimeMinuteNew.currentItem = endMinute

        } else {
            val calendar = Calendar.getInstance()
            val hour = calendar.get(Calendar.HOUR_OF_DAY)
            val minute = calendar.get(Calendar.MINUTE)
            startTime_HourNew.currentItem = hour
            startTime_MinuteNew.currentItem = minute
            endTimeHourNew.currentItem = hour
            endTimeMinuteNew.currentItem = minute
        }
        btn_time_confirm.setOnClickListener(this)
        playRandomVoiceWithEmotion(voices)
    }

    override fun onClick(v: View) {
        var returnData = returnData()
        if (returnData) {
            dismiss()
        }
    }

    private fun returnData(): Boolean {
        if (startTime_HourNew.currentItem < endTimeHourNew.currentItem) {
            checkOk()
            return true
        } else if (startTime_HourNew.currentItem == endTimeHourNew.currentItem) {
            if (startTime_MinuteNew.currentItem < endTimeMinuteNew.currentItem) {
                checkOk()
                return true
            } else {
                ToastTools.newInstance(this.context).showText(context.getString(R.string.time_select_error))
            }
        } else {
            ToastTools.newInstance(this.context).showText(context.getString(R.string.time_select_error))
        }
        return false
//        checkOk()
//        return true
    }

    private fun checkOk() {
        var startTime: Calendar = Calendar.getInstance()
        var endTime: Calendar = Calendar.getInstance()
        // Log.e(TAG, "${startTime_HourNew.currentItem}:${startTime_MinuteNew.currentItem} -- ${endTimeHourNew.currentItem}:${endTimeMinuteNew.currentItem}")
        startTime.set(Calendar.HOUR_OF_DAY, startTime_HourNew.currentItem)
        startTime.set(Calendar.MINUTE, startTime_MinuteNew.currentItem)
        endTime.set(Calendar.HOUR_OF_DAY, endTimeHourNew.currentItem)
        endTime.set(Calendar.MINUTE, endTimeMinuteNew.currentItem)
        // Log.e(TAG, "${startTime.get(Calendar.HOUR_OF_DAY)}:${startTime.get(Calendar.MINUTE)} - ${endTime.get(Calendar.HOUR_OF_DAY)}:${endTime.get(Calendar.MINUTE)}")
        var list = ArrayList<Calendar>()
        list.add(startTime)
        list.add(endTime)
        iDialogBtnLisenter?.onConfirm(list)
    }

    override fun setDialogWindowAttr() {
        setCanceledOnTouchOutside(true)
        val window = this.window
        val lp = window.attributes
        lp.gravity = Gravity.CENTER
        lp.width = this.context.resources.getDimension(R.dimen.px880).toInt()//宽高可设置具体大小
        lp.height = this.context.resources.getDimension(R.dimen.px561).toInt()
        this.window.attributes = lp
    }
}
